﻿using MediatR;
using Tb.App.API.Applications.Command.Basic;
using Tb.App.Domain.Rabbit.Basic;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Basic
{
    /// <summary>
    /// 删除溯源基本信息-命令
    /// </summary>
    public class DelBasicCommandHandler:IRequestHandler<DelBasicCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<BasicModel> basicRep;

        public DelBasicCommandHandler(IBaseRepository<BasicModel> basicRep)
        {
            this.basicRep = basicRep;
        }

        public async Task<ApiResult<int>> Handle(DelBasicCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            
            var res=await basicRep.GetById(request.Id);
            if (res == null) 
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.删除失败,
                    Message = "该数据不存在!!!",
                    Data = -1
                };
            }
            res.IsDel = true;
            var entity=await basicRep.UpdateAsync(res);
            if(entity>0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.删除成功,
                    Message = "删除成功!!!",
                    Data = entity
                };
            }
            else
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.删除失败,
                    Message = "删除失败!!!",
                    Data = entity,
                };
            }
        }
    }
}
